package com.ase.springsecurity.service.impl;

import com.ase.springsecurity.entity.mongodb.Blog;
import com.ase.springsecurity.service.BlogService;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author gyhstart
 * @create 2020/12/27 - 22:35
 **/
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * MongoDB 增加
     *
     * @param blog
     */
    @Override
    public int saveBlogMongoDB(Blog blog) {
        try {
            Blog blogSave = new Blog()
                    .setTitle(blog.getTitle())
                    .setContent(blog.getContent())
                    .setAuthor(blog.getAuthor())
                    .setDatetime(new Date())
                    .setCategory(blog.getCategory())
                    .setThumb_up(blog.getThumb_up())
                    .setCollection(blog.getCollection())
                    .setAttention(blog.getAttention())
                    .setBrowse(blog.getBrowse());
            mongoTemplate.insert(blogSave, "blog");
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * MongoDB 删除
     *
     * @param id
     */
    @Override
    public void removeBlogMongoDB(long id) {
        Query query = new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query, Blog.class);
    }

    /**
     * MongoDB 修改
     *
     * @param blog
     */
    @Override
    public long modifyBlogMongoDB(Blog blog) {
        Query query = new Query(Criteria.where("id").is(blog.getId()));
        Update update = new Update()
                .set("title", blog.getTitle())
                .set("content", blog.getContent())
                .set("author", blog.getAuthor())
                .set("datetime", new Date())
                .set("category", blog.getCategory())
                .set("thumb_up", blog.getThumb_up())
                .set("collection", blog.getCollection())
                .set("attention", blog.getAttention())
                .set("browse", blog.getBrowse());
        //更新查询返回的结果集的第一条数据
        UpdateResult result = mongoTemplate.updateFirst(query, update, Blog.class);
        //更新查询到的所有结果集
        //UpdateResult allResult = mongoTemplate.updateMulti(query,update,User.class);
        if (result != null) {
            return result.getModifiedCount();
        } else {
            return 0;
        }
    }

    /**
     * MongoDB 查询所有
     *
     * @return
     */
    @Override
    public List<Blog> queryAllBlogMongoDB() {
        return mongoTemplate.findAll(Blog.class);
    }

    /**
     * MongoDB 模糊查询
     *
     * @param title
     * @return
     */
    @Override
    public List<Blog> queryFuzzyBlogMongoDB(String title) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        Pattern pattern = Pattern.compile("^.*" + title + ".*$", Pattern.CASE_INSENSITIVE);
        query.addCriteria(criteria.and("title").regex(pattern));
        return mongoTemplate.find(query, Blog.class);
    }
}
